<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Price_Settlement extends ORM
{
    /**
     * @param $type_id
     * @param $suit_id
     * @param $settlement_type
     * 保存套餐时 结算模式 是按销售价结算时 清除表中的利润价 使售价等于成本
     */
    public static function suit_price_modify($type_id,$suit_id,$settlement_type)
    {
        $arr = array(1,2,3,5,13,114,118,121);
        if($settlement_type==2 && (in_array($type_id,$arr) || $type_id>199)){
            $table = St_Functions::table_field($type_id);
            $type_id = $type_id>199?200:$type_id;
            switch ($type_id)
            {
                case 1:
                    $data = array(
                        'adultprofit'    => 0,
                        'childprofit'    => 0,
                        'oldprofit'      => 0,
                        'people4_profit' => 0,
                        'people5_profit' => 0,
                        'people6_profit' => 0,
                        'adultprice'     => 'adultbasicprice',
                        'childprice'     => 'childbasicprice',
                        'oldprice'       => 'oldbasicprice',
                        'people4_price'  => 'people4_basicprice',
                        'people5_price'  => 'people5_basicprice',
                        'people6_price'  => 'people6_basicprice',
                    );
                    break;
                case 2:
                case 200:
                    $data = array(
                        'profit' => 0,
                        'price'  => 'basicprice',
                    );
                    break;
                case 114:
                    $data = array(
                        'profit'      => 0,
                        'childprofit' => 0,
                        'price'       => 'basicprice',
                        'childprice'  => 'childbasicprice',
                    );
                    break;
                default:
                    $data = array(
                        'adultprofit' => 0,
                        'adultprice'     => 'adultbasicprice',
                    );
                    break;
            }

            $sql = "update sline_{$table[6]} set ";
            $sql_arr = array();
            foreach ($data as $k=>$v){
                $str = $k."=".$v;
                array_push($sql_arr,$str);
            }
            $sql .= implode(',',$sql_arr);
            $sql .= " where {$table[5]}={$suit_id}";
            DB::query(3,$sql)->execute();
        }
    }

    /**
     * @param $params
     * @throws Kohana_Exception
     * 修改结算信息
     */
    public static function suit_modify($params)
    {
        $table = St_Functions::table_field($params['type_id']);
        if($params['settlement_type']){
            $suit = array(
                'settlement_type' => $params['settlement_type'],
            );
            DB::update($table[0])->set($suit)->where("id={$params['suit_id']}")->execute();
        }

        $settlement_cfg = array(
            'type_id'         => $params['type_id'],
            'suit_id'         => $params['suit_id'],
            'channel_id'      => $params['channel_id'],
            'settlement_key'  => $params['settlement_key'],
            'settlement_val'  => $params['settlement_key']==1?bcdiv($params['settlement_val'],100,2):$params['settlement_val'],
        );
       $is_exist = DB::select()->from('supplier_settlement_config')
           ->where("type_id={$settlement_cfg['type_id']} and suit_id={$settlement_cfg['suit_id']} and channel_id={$settlement_cfg['channel_id']}")
           ->execute()->current();
       if($is_exist){
           DB::update('supplier_settlement_config')->set($settlement_cfg)->where("id={$is_exist['id']}")->execute();
       }else{
           DB::insert('supplier_settlement_config', array_keys($settlement_cfg))->values(array_values($settlement_cfg))->execute();
       }
    }
}